import java.util.concurrent.locks.ReentrantLock;

public class Test02 {
    private static int count = 0;
    private static final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        // 线程1：打印奇数
        new Thread(() -> {
            while (count < 100) {
                lock.lock();
                try {
                    if (count % 2 == 1) {
                        System.out.println("Thread 1: " + count);
                        count++;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        // 线程2：打印偶数
        new Thread(() -> {
            while (count < 100) {
                lock.lock();
                try {
                    if (count % 2 == 0) {
                        System.out.println("Thread 2: " + count);
                        count++;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }).start();
    }
}
